var http = require("http")
var Promise = require("bluebird")
var cheerio = require("cheerio")
var colors = require("colors")
// var baseUrl = 'http://www.mafengwo.cn/mdd/map/21536.html?mddid=21536&page=1'
// var baseUrl = 'http://www.mafengwo.cn/mdd/map/21536.html?href=http://www.mafengwo.cn/mdd/map/21536.html&_t=1509358171761'
var baseUrl = 'http://www.mafengwo.cn/mdd/map/21536.html?mddid=21536&page=1'


var cityAll = []

function filter(html){
	var $ = cheerio.load(html)
	// var text = $(".location .city_location span a").text()
	var clone = $(".hd_wrapper .title .current").clone()
	var text = clone.text()
	var courseData = {
		name:text
	}
	return courseData
}
function getPageAsync(url){
	return new Promise(function(resolve,reject){
		console.log('正在爬取' + url)
		http.get(url,function(res){
			var html = ""
			res.on("data",function(data){
				html += data
				// console.log(html)
			})
			res.on("end",function(){
				resolve(html)
			})
		}).on("error",function(e){
			reject(e)
			console.log("error")
		})
	})
}
Promise
  .all(baseUrl)
  .then(function(pages){
  	var coursesData = {}
  	pages.forEach(function(html){
  		coursesData = filter(html)
  	})
  	printCourseInfo(coursesData)
  })
function printCourseInfo(coursesData){
	console.log(coursesData)
}

  getPageAsync(baseUrl)